#!/usr/bin/env groovy

def call(String techStack = '') {    
     echo "======================BUILD IMAGE (TECHSTACK:${techStack})============================="
     try
     {
        if ( techStack == 'vue') {
        sh label: 'DOCKER-BUILD', returnStdout: true, script: 
        '''
        echo "npm install --registry https://registry.npm.taobao.org && npm run build:${VUE_BUILD_ENV}"       
        if [ -d "dist" ];then
            rm -rf dist
        fi

            cnpm install || yarn --registry https://registry.npm.taobao.org || npm install --registry https://registry.npm.taobao.org

            if [ ${VUE_BUILD_ENV} = "N/A" ];then
                echo "npm run build...."
                npm run build
            else
                echo "npm run build:${VUE_BUILD_ENV}...."
                npm run build:${VUE_BUILD_ENV}
            fi      
            echo "build image: ${HARBOR}/${PROJECT_NAME}/${DOCKER_NAME}:${VERSION_NUMBER}"       
        
            docker build -t ${HARBOR}/${PROJECT_NAME}/${DOCKER_NAME}:${VERSION_NUMBER} -f Dockerfile .
    
            '''
        } else if (techStack == 'springboot') {
            sh label: 'DOCKER-BUILD', returnStdout: true, script: 
            '''
            mvn clean install -Dmaven.test.skip=true
            echo "docker-compose build ${DockerBuild_PARAMETER}"
            TAG=${VERSION_NUMBER} docker-compose -f docker-compose-build.yml build ${DockerBuild_PARAMETER}
            '''
        } else {
            throw new RuntimeException("NotImplementedException; ${techStack} IS NOT SUPPORT NOW")
     }
 } 
    catch(Exception ex) {
         println("Catching the exception,");
         println(ex.toString());
         println(ex.getMessage());
         println(ex.getStackTrace());  
         currentBuild.result = 'FAILURE'
      }
      finally {
        echo "currentBuild.result ${currentBuild.result}"          
        if (currentBuild.result != 'FAILURE') {           
           echo 'This did work!'
       }
   }
      echo "===============================BUILD IMAGE DONE=============================="    
}
